<?php

namespace App\Http\Controllers\Api;

use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Models\BlindBoxRecord;
use App\Models\FrontendUser;
use App\Services\BlindBoxService;

class BlindBoxController extends Controller
{
    /** @var BlindBoxService */
    private $blindBoxService;

    public function __construct(BlindBoxService $blindBoxService)
    {
        $this->blindBoxService = $blindBoxService;
    }

    /** 查询我的盲盒 */
    public function listMyBlindBoxes()
    {
        /** @var FrontendUser $user */
        $user = auth()->user();
        $page = request()->page ?: 1;
        $limit = request()->limit ?: 10;

        $query = BlindBoxRecord::query()
            ->where('user_id', $user->id)
            ->whereNull('user_asset_id')
            ->orderByDesc('id');
        $count = $query->count();
        $list = $query
            ->with(['blind_box_product', 'asset_product', 'asset'])
            ->orderByDesc('id')
            ->offset(($page - 1) * $limit)->limit($limit)
            ->get()->map(function (BlindBoxRecord $record) {
                return [
                    'id' => $record->id,
                    'product_name' => $record->blind_box_product->name,
                    'product_logo_url' => $record->blind_box_product->logo_url,
                    'create_time' => $record->created_at->toDateTimeString(),
                    'auto_open_time' => $record->created_at->addMinutes(10)->toDateTimeString(),
                ];
            });

        return [
            'count' => $count,
            'list' => $list,
        ];
    }

    /** 开启盲盒 */
    public function openBlindBox($id)
    {
        /** @var FrontendUser $user */
        $user = auth()->user();

        /** @var BlindBoxRecord $record */
        $record = BlindBoxRecord::query()
            ->where('user_id', $user->id)
            ->find($id);
        if (!$record) {
            throw new ApiException('盲盒记录未找到');
        }
        if ($record->asset) {
            return $this->blindBoxRecordToArray($record);
        }

        try {
            $this->blindBoxService->openBlindBox($record);
        } catch (\Exception $e) {
            throw new ApiException('盲盒开启失败:' . $e->getMessage());
        }

        return $this->blindBoxRecordToArray($record);
    }

    /** 查询我的盲盒开启记录 */
    public function listBlindBoxOpenRecords()
    {
        /** @var FrontendUser $user */
        $user = auth()->user();
        $page = request()->page ?: 1;
        $limit = request()->limit ?: 10;

        $query = BlindBoxRecord::query()
            ->whereHas('asset')
            ->where('user_id', $user->id)
            ->orderByDesc('open_time');
        $count = $query->count();
        $list = $query
            ->with(['blind_box_product', 'asset_product', 'asset'])
            ->orderByDesc('id')
            ->offset(($page - 1) * $limit)->limit($limit)
            ->get()->map(function (BlindBoxRecord $record) {
                return $this->blindBoxRecordToArray($record);
            });

        return [
            'count' => $count,
            'list' => $list,
        ];
    }

    private function blindBoxRecordToArray(BlindBoxRecord $record)
    {
        $blindBox = $record->blind_box_product;
        $assetProduct = $record->asset_product;
        $asset = $record->asset;

        return [
            'product_logo_url' => $blindBox->logo_url,
            'product_name' => $blindBox->name,
            'create_time' => $record->created_at->toDateTimeString(),
            'open_time' => $record->open_time,
            'asset_product_id' => $assetProduct->id,
            'asset_contract_address' => $assetProduct->contract_address,
            'asset_name' => $assetProduct->name,
            'asset_logo_url' => $assetProduct->logo_url,
            'asset_cert_sn' => $asset->cert_sn,
        ];
    }
}
